package com.pig4cloud.pigx.wallet.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.mysql.utils.MysqlUtil;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.user.entity.User;
import com.pig4cloud.pigx.wallet.api.feign.RemoteRippleUserService;
import com.pig4cloud.pigx.wallet.constant.WalletConstant;
import com.pig4cloud.pigx.wallet.constant.WalletStatusEnum;
import com.pig4cloud.pigx.wallet.dto.ChargeMentionNotificationDTO;
import com.pig4cloud.pigx.wallet.dto.UserRebateDTO;
import com.pig4cloud.pigx.wallet.entity.CapitalAccount;
import com.pig4cloud.pigx.wallet.entity.Notification;
import com.pig4cloud.pigx.wallet.entity.RechargeRecord;
import com.pig4cloud.pigx.wallet.mapper.CapitalAccountMapper;
import com.pig4cloud.pigx.wallet.mapper.RechargeRecordMapper;
import com.pig4cloud.pigx.wallet.mapstruct.NotificationStruct;
import com.pig4cloud.pigx.wallet.service.NotificationService;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 新用户返利 定时任务
 *
 */
@Component
@EnableScheduling
@AllArgsConstructor
public class UserRebateTask {

	private final RechargeRecordMapper recordMapper;

	private final CapitalAccountMapper accountMapper;

	private final CommonUtil commonUtil;
	private final MysqlUtil mysqlUtil;

	/**
	 * 用户消息通知
	 */
	private final NotificationService notificationService;

	private final RemoteRippleUserService remoteRippleUserService;


	/**
	 * 每小时一查看一结算
	 */
	@Scheduled(cron = "0 0 * * * ?")
	public void rebateTask() {
		List<UserRebateDTO> list = recordMapper.selectUserRebate();
		for (UserRebateDTO dto : list) {
			try {
				mysqlUtil.startLocalTx();
				CapitalAccount account = accountMapper.selectOne(new QueryWrapper<CapitalAccount>() {{
					eq("user_id", dto.getUserId());
					last("for update");
				}});
				BigDecimal rebate = dto.getRebate();
				// 返利首充金额的10%
				rebate = CommonUtil.setUSDTScale(rebate.divide(BigDecimal.TEN, 8, RoundingMode.HALF_UP));
				// 最多返利100U
				if (rebate.compareTo(new BigDecimal("100")) > 0) {
					rebate = new BigDecimal("100");
				}
				// 新增资金账户充值记录
				RechargeRecord record = new RechargeRecord();
				record.setId(commonUtil.getId());
				record.setQuantity(rebate);
				record.setState(WalletStatusEnum.RECHARGE_COMPLETED.getStatus());
				record.setUserId(dto.getUserId());
				record.setCurrency(WalletConstant.USDT);
				record.setType("4");
				recordMapper.insert(record);

				// 封装充提通知参数
				R<User> user = remoteRippleUserService.getUserByUserId(record.getUserId(), SecurityConstants.FROM_IN);
				ChargeMentionNotificationDTO notificationDTO = new ChargeMentionNotificationDTO(record);
				notificationDTO.setLanguage(user.getData().getLanguageCode());
				// 用户消息通知
				Notification notification = NotificationStruct.INSTANCE.chargeMentionDTO2PO(notificationDTO);
				notificationService.save(notification);

				// 资金账户变更
				account.setAvailable(account.getAvailable() == null ? rebate : account.getAvailable().add(rebate));
				accountMapper.updateById(account);
				mysqlUtil.commitLocalTx();
			} catch (Exception e) {
				mysqlUtil.rollbackLocalTx();
			}
		}
	}

}
